package com.raports;

import com.wizardOther.*;
import java.util.HashMap;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.beans.property.DoubleProperty;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.layout.StackPane;
import javafx.util.Duration;

/**
 *
 * @author Sawior
 */
public class RaportScreensController extends StackPane {

    private HashMap<String, Node> screens = new HashMap<>();
    private HashMap<String, RaportControlledScreen> controllers = new HashMap<>();
    private RaportMenuWrapperScreenController raportMenuScreen;
    private Boolean firstOpen = true;

    public RaportScreensController(RaportMenuWrapperScreenController raportMenuScreen) {
        super();
        this.raportMenuScreen = raportMenuScreen;
    }

    public void addScreen(String name, Node screen) {
        screens.put(name, screen);
    }

    public boolean loadScreen(String name, String resource) {
        try {
            FXMLLoader myLoader = new FXMLLoader(getClass().getClassLoader().getResource(resource));
            Parent loadScreen = (Parent) myLoader.load();
            RaportControlledScreen myScreenControler = ((RaportControlledScreen) myLoader.getController());
            myScreenControler.setScreenParent(this);
            addScreen(name, loadScreen);
            controllers.put(name, myScreenControler);
            return true;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return false;
        }
    }

    public boolean setScreen(final String name) {

        if (screens.get(name) != null) {
            final DoubleProperty opacity = opacityProperty();

            if (!getChildren().isEmpty()) {
                Timeline fade = new Timeline(
                        new KeyFrame(Duration.ZERO,
                                new KeyValue(opacity, 1.0)),
                        new KeyFrame(new Duration(500),
                                new EventHandler() {
                                    @Override
                                    public void handle(Event t) {
                                        getChildren().remove(0);
                                        getChildren().add(0, screens.get(name));
                                        Timeline fadeIn = new Timeline(
                                                new KeyFrame(Duration.ZERO,
                                                        new KeyValue(opacity, 0.0)),
                                                new KeyFrame(new Duration(400),
                                                        new KeyValue(opacity, 1.0)));
                                        fadeIn.play();
                                    }
                                }, new KeyValue(opacity, 0.0)));
                fade.play();
            } else {
                setOpacity(0.0);
                getChildren().add(screens.get(name));
                Timeline fadeIn = new Timeline(
                        new KeyFrame(Duration.ZERO,
                                new KeyValue(opacity, 0.0)),
                        new KeyFrame(new Duration(2500),
                                new KeyValue(opacity, 1.0)));
                fadeIn.play();
            }
            if(firstOpen){
                firstOpen = false;
                return true;
            }
            if (name.equals(RaportScreensFramework.RAPORT_BUTTONS)) {
                raportMenuScreen.setOtherButtonsShow(true);
            } else {
                raportMenuScreen.setOtherButtonsShow(false);
            }
            raportMenuScreen.setSecondBreadCrumbsText(name);
            return true;
        } else {
            System.out.println("screen hasn't been loaded!\n");
            return false;
        }
    }

    public boolean unloadScreen(String name) {
        if (screens.remove(name) == null) {
            System.out.println("Screen didn't exist");
            return false;
        } else {
            return true;
        }
    }

    public RaportMenuWrapperScreenController getOtherMenuScreen() {
        return raportMenuScreen;
    }
    
    public RaportControlledScreen getController(String name){
        return this.controllers.get(name);
    }

}
